package com.fengye.feign;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.fengye.feign.api.UploadMata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@SpringBootApplication
@EnableDiscoveryClient

public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
    @RestController
    class TestController {

        private static final Logger logger = LoggerFactory.getLogger(TestController.class);
        private static final String FILE_PATH = "C:\\dev";

        // 测试接口
        @GetMapping("/provider/test/{name}")
        public String test(@PathVariable(value = "name") String name) {
            return "hello " + name;
        }

        @PostMapping("/provider/upload")
        @ResponseBody
        public UploadMata upload(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "meta") String meta) {
            logger.info("接收到文件: {}, 元数据: {}", file.getOriginalFilename(), meta);
            UploadMata uploadMata = JSONUtil.toBean(meta, UploadMata.class);
            // 处理文件上传逻辑
            try {
                byte[] bytes = file.getBytes();
                FileUtil.writeBytes(bytes, FILE_PATH + FileUtil.FILE_SEPARATOR + uploadMata.getFileName());
                return uploadMata;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
